﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;

namespace ConsoleServer.Properties
{
    class FirstServer
    {
        const int BufferSize = 8192;
        string ip = "192.168.11.3";
        int port = 8500;

        public FirstServer()
        {
            IPAddress address = IPAddress.Parse(ip);
            TcpListener listener = new TcpListener(address, port);

            listener.Start();
            Console.WriteLine("服务器开始侦听");

            while (true)
            {
                //等待客户端连接
                TcpClient client = listener.AcceptTcpClient();
                Console.WriteLine(client.Client.RemoteEndPoint + " -> 连接成功");

                //获取网络流
                NetworkStream stream = client.GetStream();

                while (true)
                {
                    byte[] buffer = new byte[BufferSize];
                    int readCount = stream.Read(buffer, 0, BufferSize);
                    string msg = Encoding.UTF8.GetString(buffer, 0, readCount);

                    Console.WriteLine("接收到消息 -> " + msg);
                }
            }

            Console.WriteLine("输入任意键退出");
            Console.ReadLine();
        }
    }
}
